package com.baosan.school.service.ipml;

import com.baosan.school.common.RedisUtil;
import com.baosan.school.mapper.UserMapper;
import com.baosan.school.po.Result;
import com.baosan.school.po.ResultEnum;
import com.baosan.school.po.UserPO;
import com.baosan.school.service.UserService;
import com.github.pagehelper.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Base64;

/**
 * @program: school
 * @description:
 * @author: heping
 * @create: 2022-01-16 06:32
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RedisUtil redisUtil;

    @Override
    public  Result<UserPO> checkPwd(UserPO po) {
        Result<UserPO> result = new Result<>();
        UserPO userPO;
        result.setCode(ResultEnum.SUCCESS.getKey());
        if(StringUtil.isEmpty(po.getId())){
            result.setCode(ResultEnum.ERROR.getKey());
            result.setMsg("请输入学号");
        }else{
            userPO = userMapper.getUser(po);
            if(userPO.getUserPwd().equals(po.getUserPwd())){
                String token = Base64.getEncoder().encodeToString(po.getId().getBytes());
                redisUtil.set(userPO.getId(),token,2*60*60);
                userPO.setToken(token);
                result.setData(userPO);
                result.setMsg("登陆成功");
            }else{
                result.setCode(ResultEnum.ERROR.getKey());
                result.setMsg("密码错误");
            }
        }
        return result;
    }
}

